package com.household.controller;

import com.household.domain.ResponseResult;
import com.household.domain.entity.Company;
import com.household.domain.entity.User;
import com.household.domain.enums.ResponseErrorCodeEnum;
import com.household.domain.enums.RoleEnum;
import com.household.domain.enums.SexEnum;
import com.household.mapper.CompanyMapper;
import com.household.service.CompanyService;
import com.household.service.UserService;
import com.household.utils.IDValidateUtils;
import com.household.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created By wanzhen
 * 2023. 2023/3/25. 14:55
 */

@Slf4j
@RestController
public class AuthController {
    private final CompanyMapper companyMapper;

    private final CompanyService companyService;

    private final UserService userService;

    @Autowired
    public AuthController(CompanyMapper companyMapper, CompanyService companyService, UserService userService) {
        this.companyMapper = companyMapper;
        this.companyService = companyService;
        this.userService = userService;
    }

    @PostMapping("/auth/register")
    public ResponseResult register(Integer type, String username, String password,String tel, String code, HttpSession session) {
        if (type==null){
            return ResponseResult.failure(ResponseErrorCodeEnum.PARAMETER_ERROR);
        }
        switch (type){
            case 1:
                if (StringUtils.isAnyBlank(username, password)) {
                    return ResponseResult.failure(ResponseErrorCodeEnum.PARAMETER_ERROR);
                }
                return userService.registryByUsername(username, password);
            default:
                return ResponseResult.failure(ResponseErrorCodeEnum.PARAMETER_ERROR);
        }
    }

    /**
     * 信息补全
     */
    @PostMapping("/auth/complete-info")
    public ResponseResult completeInfo(Integer role,String idCard,String realName,Integer sex,String company,@AuthenticationPrincipal User user){
        if (role==null||sex==null||StringUtils.isBlank(idCard)||StringUtils.isBlank(realName)){
            return ResponseResult.failure(ResponseErrorCodeEnum.PARAMETER_ERROR);
        }
//        仅支持申请普通用户和家政公司人员
        RoleEnum roleEnum=RoleEnum.getByType(role);
        if (roleEnum!=RoleEnum.USER&&roleEnum!=RoleEnum.COMPANY){
            return ResponseResult.failure(ResponseErrorCodeEnum.PARAMETER_ERROR);
        }
        user.setRole(roleEnum);
//        校验性别
        SexEnum sexEnum=SexEnum.getByType(sex);
        if (sexEnum==null){
            return ResponseResult.failure(ResponseErrorCodeEnum.PARAMETER_ERROR);
        }
        user.setSex(sexEnum);

//        填写真实姓名和身份证号
        if (!IDValidateUtils.check(idCard)){
            return ResponseResult.failure(ResponseErrorCodeEnum.ID_CARD_INVALID);
        }
        if (StringUtils.containsSpecial(realName)||StringUtils.containsNumber(realName)){
            return ResponseResult.failure(ResponseErrorCodeEnum.REAL_NAME_INVALID);
        }
        user.setIdCard(idCard);
        user.setRealName(realName);
        user.setCompanyId(company);

        if (!userService.updateById(user)){
            return ResponseResult.failure(ResponseErrorCodeEnum.OPERATION_ERROR);
        }
        return ResponseResult.success();
    }

    @GetMapping("/auth/company")
    public ResponseResult getAllCompany(){
        List<Company> companies = companyMapper.selectList(null);
        return ResponseResult.success(companies);

    }
}

